package com.kt.kvw.user.service.impl;

import com.kt.kvw.enums.ResultCodeEnum;
import com.kt.kvw.generator.IDGenerator;
import com.kt.kvw.user.dao.KwvUserMapper;
import com.kt.kvw.model.domain.KwvUser;
import com.kt.kvw.user.model.form.RegisterForm;
import com.kt.kvw.user.service.UserService;
import com.kt.kvw.util.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Desc:
 *
 * @author: keen
 * Date: 2020-07-05
 * Time: 14:52
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private KwvUserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    /**
     * 注册
     *
     * @param registerForm 用户信息
     * @return KwvUser
     */
    @Override
    public int register(RegisterForm registerForm) {
        log.info(registerForm.toString());
        KwvUser params = new KwvUser();
        params.setPhone(registerForm.getPhone());
        KwvUser user = userMapper.selectUser(params);
        if (user != null) {
            ExceptionUtils.cast(ResultCodeEnum.PHONE_EXISTS);
        }
        KwvUser kwvUser = new KwvUser();
        kwvUser.setUserId(IDGenerator.uuid());
        kwvUser.setUserName("萌新一枚");
        kwvUser.setPhone(registerForm.getPhone());
        kwvUser.setGender(true);
        kwvUser.setCreatedDate(new Date());
        kwvUser.setCreatedBy(kwvUser.getUserId());
        kwvUser.setPassword(passwordEncoder.encode(registerForm.getPassword()));
        return userMapper.insert(kwvUser);
    }

    /**
     * 登录
     *
     * @param userName
     * @param password
     */
    @Override
    public void login(String userName, String password) {

    }
}
